package com.bamboowind.service.impl;

import com.bamboowind.mapper.OrderInfoMapper;
import com.bamboowind.pojo.OrderInfo;
import com.bamboowind.service.OrderInfoService;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.github.pagehelper.PageHelper;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.session.RowBounds;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @author Blue
 */
@Service
public class OrderInfoServiceImpl extends ServiceImpl<OrderInfoMapper, OrderInfo> implements OrderInfoService {

    @Autowired
    private OrderInfoMapper orderInfoMapper;

    /**
     * 原生Limit分页
     *
     * @param pageNow
     * @param pageSize
     * @return
     */
    @Override
    public List<OrderInfo> getOrderInfoListByLimit(int pageNow, int pageSize) {
        /**
         * 解释：这里这么做的原因是mysql的起始索引从0开始
         * 起始索引=（当前要查询的页码-1）*每页显示的记录数
         * 例如：你要查询第2页的3条数据，（2-1）*3=3 mysql执行limit 3,3
         * 所以就成功分为0,1,2为第一页，3,4,5为第二页
         * */
        if (pageNow == 1) {
            pageNow = 0;
        }
        return orderInfoMapper.getOrderInfoListByLimit(pageNow, pageSize);
    }

    /**
     * RowBounds分页
     *
     * @param pageNow
     * @param pageSize
     * @return
     */
    @Override
    public List<OrderInfo> getOrderInfoListByRowBounds(int pageNow, int pageSize) {
        RowBounds rowBounds = new RowBounds(pageNow, pageSize);
        return orderInfoMapper.getOrderInfoListByRowBounds(rowBounds);
    }

    /**
     * 分页插件PageHelper分页
     *
     * @param pageNow
     * @param pageSize
     * @return
     */
    @Override
    public List<OrderInfo> getOrderInfoListByPageHelper(int pageNow, int pageSize) {
        PageHelper.startPage(pageNow, pageSize);
        List<OrderInfo> orderInfoList = orderInfoMapper.getOrderInfoListByPageHelper();
        PageInfo<OrderInfo> pageInfo = new PageInfo<>(orderInfoList);
        return pageInfo.getList();
    }

    /**
     * MP自带的分页拦截器进行分页
     *
     * @param pageNow
     * @param pageSize
     * @return
     */
    @Override
    public IPage<OrderInfo> getOrderInfoListByMp(int pageNow, int pageSize) {
        IPage page = new Page(pageNow, pageSize);
        orderInfoMapper.selectPage(page, null);
        return page;
    }
}
